package com.wuzf.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import com.alibaba.fastjson.JSONArray;
import com.wuzf.dao.UserDao;
import com.wuzf.entity.Permission;
import com.wuzf.entity.Role;
import com.wuzf.entity.User;
import com.wuzf.utils.JdbcTemplateUtils;

public class UserDaoImpl implements UserDao{
	
	private JdbcTemplate template = JdbcTemplateUtils.JdbcTemplate();

	public User createUser(final User user) {
		String sql = "insert into sys_users (username, password, salt, locked) values (?, ?, ?, ?)";
		template.update(sql, new PreparedStatementSetter() {
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1, user.getUsername());
				ps.setString(2, user.getPassword());
				ps.setString(3, user.getSalt());
				ps.setBoolean(4, user.getLocked());
			}
		});
		User u = findByUsername(user.getUsername());
		user.setId(u.getId());
		return user;
	}

	public void updateUser(User user) {
		// TODO Auto-generated method stub
		
	}

	public void deleteUser(Long userId) {
		// TODO Auto-generated method stub
		
	}

	public void correlationRoles(Long userId, Long... roleIds) {
		if (roleIds == null || roleIds.length == 0) {
			return;
		}
		String sql = "insert into sys_users_roles (user_id, role_Id) values (?, ?)";
		for (Long roleId : roleIds) {
			if (!exists(userId, roleId)) {
				template.update(sql, userId, roleId);
			}
		}
	}
	
	public Boolean exists(Long userId, Long roleId) {
		String sql = "select count(1) from sys_users_roles where user_id = ? and role_id = ? ";
		return template.queryForObject(sql, Integer.class, userId, roleId) != 0;
	}

	public void uncorrelationRoles(Long userId, Long... roleIds) {
		// TODO Auto-generated method stub
		
	}

	public User findOne(Long userId) {
		String sql = "select id, username, password, salt, locked from sys_users where id=?";
		List<User> list = template.query(sql, new BeanPropertyRowMapper(User.class) ,userId); 
		if (list.size() == 0) {
			return null; 
		} else {
			return list.get(0);
		}
	}

	public User findByUsername(String username) {
		String sql = "select * from sys_users where username = ? ";
		List<User> userList = template.query(sql, new BeanPropertyRowMapper(User.class), username);
	    if (userList.size() == 0) {
	        return null;
	    }
	    return userList.get(0);
	}

	public Set<String> findRoles(String username) {
		System.out.println("用户名：" + username);
		String sql = "select m.* from sys_roles m, sys_users n, "
				+ "sys_users_roles mn where n.id = mn.user_id and mn.role_id = m.id and n.username = ? ";
		List<Role> list = template.query(sql, new BeanPropertyRowMapper(Role.class), username);
		if (list.size() == 0) {
			return null;
		}
		
        Set<String> set=new HashSet<String>();         
        for (Role role : list) {
        	set.add(role.getRole());
        }      
		return set;
	}

	public Set<String> findPermissions(String username) {
		System.out.println("用户名：" + username);
		String sql = "select m.* from sys_permissions m, "
				+ "sys_users n, sys_roles_permissions mn, sys_users_roles ss "
				+ "where n.id = ss.user_id and ss.role_id = mn.role_id and "
				+ "mn.permission_id = m.id and n.username = ? ";
		List<Permission> list = template.query(sql, new BeanPropertyRowMapper(Permission.class), username);
		if (list.size() == 0) {
			return null;
		}
		Set<String> set=new HashSet<String>();         
        for (Permission permission : list) {
        	set.add(permission.getPermission());
        }    
		return set;
	}
	
}
